Repository navigation
[Feature] Add sanitize shop param utility - #483
Conversation
e650e5a to
7864dd7
Compare
dca60e2 to
e5e522e
Compare
| if myshopify_domain not in name and "." not in name: | ||
| name += ".{domain}".format(domain=myshopify_domain) | ||
| name = re.sub(r"https?://", "", name) | ||
|
|
||
| uri = urlparse("http://{hostname}".format(hostname=name)) |
There was a problem hiding this comment.
I wonder if we can go with a single RE here (untested):
| if myshopify_domain not in name and "." not in name: | |
| name += ".{domain}".format(domain=myshopify_domain) | |
| name = re.sub(r"https?://", "", name) | |
| uri = urlparse("http://{hostname}".format(hostname=name)) | |
| matches = re.match(r'(https?://)?({h})(\.{d})?'.format(h=HOSTNAME_PATTERN, d=myshopify_domain), shop_domain.lower().strip()) | |
| if matches: | |
| name = matches.group(2) + myshopify_domain | |
| return name |
There was a problem hiding this comment.
I was playing around with this:
print re.match(r'(https?://)?({h})(\.{d})?'.format(h=r"[a-z0-9][a-z0-9-]*[a-z0-9]", d='myshopify.com'), 'https://test.myshopify.com'.lower().strip()).group(1,2,3)
('https://', 'test', 'myshopify.com')print re.match(r'(https?://)?({h})(\.{d})?'.format(h=r"[a-z0-9][a-z0-9-]*[a-z0-9]", d='myshopify.com'), 'https://test'.lower().strip()).group(1,2,3)
('https://', 'test', None)Seems to work! (Note that I've changed the regex slightly from my initial suggestion)
There was a problem hiding this comment.
This unfortunately has trouble dealing with some of these bad input cases:
"myshop.com",
"myshopify.com",
"shopify.com",
"two words",
"store.myshopify.com.evil.com",
"/foo/bar",
"/foo.myshopify.io.evil.ru",
"%0a123.myshopify.io ",
"foo.bar.myshopify.io",
Edit: Sorry, this is replying to the first comment
There was a problem hiding this comment.
Hmm yeah the regexes above fail for the following inputs:
shopify.com: returnsshopifymyshopify.com(expectedNone)myshop.com: returnsmyshopmyshopify.com(expectedNone)two words: returnstwomyshopify.com(expectedNone)foo.bar.myshopify.io: returnsfoomyshopify.com(expectedNone)
There was a problem hiding this comment.
Ah yes, my regex didn't account for cases where the . in the domain isn't the start for .myshopify.com. That requirement is contextual, so we probably won't be able to handle it with a straight-up regexp. We could rewrite this code around a regex, but I think it would end up looking pretty much the same. LGTM!
7864dd7 to
edfa11a
Compare
| if myshopify_domain not in name and "." not in name: | ||
| name += ".{domain}".format(domain=myshopify_domain) | ||
| name = re.sub(r"https?://", "", name) | ||
|
|
||
| uri = urlparse("http://{hostname}".format(hostname=name)) |
There was a problem hiding this comment.
Ah yes, my regex didn't account for cases where the . in the domain isn't the start for .myshopify.com. That requirement is contextual, so we probably won't be able to handle it with a straight-up regexp. We could rewrite this code around a regex, but I think it would end up looking pretty much the same. LGTM!
edfa11a to
613f6c1
Compare
fbdf25a to
c95ba2b
Compare
8dd0cf1 to
68fea6d
Compare
c95ba2b to
8bc8132
Compare
68fea6d to
984849a
Compare
Why are these changes introduced?
This PR adds a
sanitize_shop_domainutility method that can be used repeatedly throughout the lifecycle of an app's Oauth with Shopify.This implementation has been ported from the shopify_app gem: https://github.com/Shopify/shopify_app/blob/17138d7d29d0c85d8f38ace981cac5f726b68833/lib/shopify_app/utils.rb#L4-L14
Developers can use this like so:
Things to focus on
urllibChecklist